/*
	Object Control Center
*/

objectdef obj_Core
{
	variable string version = "ALPHA Rev .00x"
	variable string CurrentState
	variable int Pulse
	variable int CoreAttached = 0
	
	method Initialize()
	{
		echo "HNBot Core ${This.version} Initialized"
	}
	
	method Shutdown()
	{
		echo "HNBot Core ${This.version} Shutting Down"
	}
		
	method CorePulse()
	{
		Pulse:Inc
		variable int IntervalInSeconds = 2
		
		if ${Pulse} >= ${Math.Calc[${Display.FPS} * ${IntervalInSeconds}]}
		{
			if ((${This.GameHour} == 12 && ${This.GameMinute} <= 10) || (${This.GameHour} == 11) || (${This.GameHour} == 10 && ${This.GameMinute} >= 50) && ${Me.Ship(exists)})
			{
				echo "Time to logout"
				Call This.DeattachCore
			}
			FrameCounter:Set[0]
		}
	}
	
	function Main()
	{
		do
		{
			wait 100
		}
		while TRUE
	}
	
	function AttachCore()
	{
		echo "HNBot Core ${This.version} Attaching"
		UI -Load "${Script.CurrentDirectory}/ui/ui.xml"
		Event[OnFrame]:DetachAtom[LoginScript:StatePulse]
		Event[OnFrame]:AttachAtom[Drone:StatePulse]
		Event[OnFrame]:AttachAtom[This:CorePulse]
		This.CoreAttached:Set[1]
	}
	
	function DeattachCore()
	{
		Event[OnFrame]:DetachAtom[This:CorePulse]
		Event[OnFrame]:DetachAtom[Drone:StatePulse]
		endscript EVEBot
		call This.ReturnAllToDroneBay
		call This.Dock
	}
	
	member:int GameHour()
	{
		variable string Hour = ${EVE.Time[short].Token[1, :]}
		variable int HourInt = ${Hour}	
		return ${HourInt}	
	}
	
	member:int GameMinute()
	{
		variable string Minute = ${EVE.Time[short].Token[2, :]}
		variable int MinuteInt = ${Minute}	
		return ${MinuteInt}	
	}
	
	function ReturnAllToDroneBay()
{
variable index:int ActiveDroneIDList

	while ${Me.GetActiveDroneIDs[This.ActiveDroneIDList]} > 0
	{
		echo "Recalling ${This.ActiveDroneIDList.Used} Drones"
		EVE:DronesReturnToDroneBay[This.ActiveDroneIDList]
		wait 50
	}	
}

function Dock()
{
	variable int Counter = 0
	variable int StationID = 0

	echo "EL: Docking"

  ;;;;;;;;;;;;;;;;;;
  ;;; Sanity Checks
	if (${StationID} <= 0)
	{
		echo "Error: oSpace::Dock --> Home Station unknown, going to nearest base"
		StationID:Set[${Entity[CategoryID,3].ID}]
	}

	if ${Entity[${StationID}].Distance} >= 10000
	{
	  echo "Warping to Station"
		if ${Entity[${Id}].Distance} >= 10000
		{
			echo "Warping to ${Entity[${Id}].Name}"
			Entity[${Id}]:WarpTo
		}
		
		do
		{ 
		   wait 20
		}
		while ${Entity[${StationID}].Distance} >= 10000
	}

	Entity[${StationID}]:Approach
	do
	{
		wait 20
	}
	while (${Entity[${StationID}].Distance} > 100)
	
	Counter:Set[0]
	echo "In Docking Range ... Docking"
	Entity[${StationID}]:Dock			
	do
	{
	   wait 20
	   Counter:Inc[20]
	   if (${Counter} > 200)
	   {
	      echo " - Docking atttempt failed ... trying again."
	      Entity[${StationID}]:Dock	
	      Counter:Set[0]
	   }
	}
	while (!${Me.InStation})					

	wait 20
	echo "Finished Docking"
}
}